<template>
  <div class="col main-content-edit mt-15">
    <el-card>
      <div slot="header" class="clearfix">
        <h5>添加分类</h5>
      </div>
      <el-form :model="category" :rules="rules" labelPosition="top" ref="form" class="custom-form">
        <el-form-item label="分类名称:" required>
          <el-row :gutter="30">
            <el-col :span="9">
              <el-input v-model="category.name" placeholder="输入分类名称"></el-input>
            </el-col>
            <el-col :span="15">
              <span>填写商品分类名称，不能为空</span>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item label="上级分类:">
          <el-row :gutter="30">
            <el-col :span="9">
              <h-good-category-choose v-model="ids"></h-good-category-choose>
            </el-col>
            <el-col :span="15">
              <span>请选择该分类的上级分类</span>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="是否显示:">
          <el-row :gutter="30">
            <el-col :span="9">
              <el-radio v-model="category.isShow" :label="1">是</el-radio>
              <el-radio v-model="category.isShow" :label="0">否</el-radio>
            </el-col>
            <el-col :span="15">
              <span>是否在商品分类选择列表中显示</span>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="分类描述:">
          <el-row :gutter="30">
            <el-col :span="9">
              <el-input type="textarea" v-model="category.descript" :rows="4"></el-input>
            </el-col>
            <el-col :span="15">
              <span>请描述分类信息</span>
            </el-col>
          </el-row>
        </el-form-item>

      </el-form>
      <div>
        <el-button type="primary" @click="submitForm"><i class="fa fa-plus"></i> 添加</el-button>
        <el-button type="success" @click="goback"><i class="fa fa-sign-out"></i> 返回</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>

  import AdminService from 'src/service/admin'
  import HGoodCategoryChoose from 'src/components/common/choose/HGoodCategoryChoose'

  export default {
    name: 'AdminCreateCategory',
    components: {HGoodCategoryChoose},
    data() {
      return {
        category: {
          name: '',
          parentId: null,
          isShow: 1,
          descript: ''
        },
        ids: [],
        rules: {}
      }
    },
    methods: {
      submitForm: function () {
        const me = this
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (me.ids.length > 0) {
              me.category.parentId = me.ids[me.ids.length - 1]
            }
            AdminService.saveCategory(me.category).then(datas => {
              if (datas != null && datas.datas.length > 0) {
                const data = datas.datas[0]
                me.$message.success('添加分类:' + data.name + '成功!')
                setTimeout(function () {
                  me.goback()
                }, 300)
              }
            })
          } else {
            me.$message.warning('请正确填写信息!')
            return false
          }
        })
      },
      submitFormMore: function () {

      },
      goback: function () {
        this.$router.go(-1)
      },
      restForm: function () {
        this.$refs['form'].resetFields()
      }
    }
  }
</script>
